import 'package:aero_frisbee/app/theme/theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get_it/get_it.dart';

import 'app/conf/app_env.dart';
import 'app/store/app_store.dart';
import 'app_route.dart';

class EntryApp extends ConsumerStatefulWidget {
  const EntryApp({super.key});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() => _EntryAppState();
}

class _EntryAppState extends ConsumerState<EntryApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      debugShowCheckedModeBanner: false,
      routerDelegate: ref.watch(appRouterProvider).routerDelegate,
      routeInformationParser:
          ref.watch(appRouterProvider).routeInformationParser,
      routeInformationProvider:
          ref.watch(appRouterProvider).routeInformationProvider,
      onGenerateTitle: (context) => AppENV.title,
      themeMode: ThemeMode.light,
      theme: AppThemeData.light,
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate, //iOS
      ],
      supportedLocales: const [
        Locale('zh', 'CN'),
        Locale('en', 'US'),
      ],
      localeResolutionCallback: (deviceLocale, supportedLocales) {
        final appStore = GetIt.I.get<AppStore>();
        if (deviceLocale != null) {
          appStore.languageCode = deviceLocale.languageCode;
        }
        return deviceLocale;
      },
      builder: FlutterSmartDialog.init(),
    );
  }
}
